using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;

namespace Zephyris_Space_Mercs
{
    class Controller
    {
        const float thumbstickAlpha = 0.01f;

        public bool drawLeftThumbstick = true;
        public bool drawRightThumbstick = true;

        public ThumbsticksValues thumbsticksVals = new ThumbsticksValues();
        public AccelerometerValues accelerometerVals = new AccelerometerValues();

        public ThumbsticksValues GetThumbSticksControl(GameTime gameTime)
        {
            VirtualThumbsticks.Update();

            thumbsticksVals.leftPos = VirtualThumbsticks.LeftThumbstick;
            thumbsticksVals.rightPos = VirtualThumbsticks.RightThumbstick;

            if (VirtualThumbsticks.LeftThumbstickCenter.HasValue)
            {
                thumbsticksVals.leftCentrePos = VirtualThumbsticks.LeftThumbstickCenter.Value;

                if (thumbsticksVals.leftCentrePos.X < Constants.deadzoneVal)
                {
                    thumbsticksVals.isLeftDeadZone = true;
                }
                else
                {
                    thumbsticksVals.isLeftDeadZone = false;
                }
            }

            if (VirtualThumbsticks.RightThumbstickCenter.HasValue)
            {
                thumbsticksVals.rightCentrePos = VirtualThumbsticks.RightThumbstickCenter.Value;

                if (thumbsticksVals.rightCentrePos.X > (TouchPanel.DisplayWidth - Constants.deadzoneVal))
                {
                    thumbsticksVals.isRightDeadZone = true;
                }
                else
                {
                    thumbsticksVals.isRightDeadZone = false;
                }
            }

            return thumbsticksVals;
        }

        public AccelerometerValues GetAccelerometerControl(GameTime gameTime)
        {
            VirtualAccelerometer.Update();

            accelerometerVals.centrePos = VirtualAccelerometer.AccelerometerValue;

            return accelerometerVals;
        }

        public void DrawControls(SpriteBatch spriteBatch, Texture2D thumbstick)
        {
            if (VirtualThumbsticks.LeftThumbstickCenter.HasValue && !thumbsticksVals.isLeftDeadZone && drawLeftThumbstick)
            {
                spriteBatch.Draw(
                    thumbstick,
                    VirtualThumbsticks.LeftThumbstickCenter.Value - new Vector2(thumbstick.Width / 2f, thumbstick.Height / 2f),
                    new Color(0, 128, 0, thumbstickAlpha));
            }

            if (VirtualThumbsticks.RightThumbstickCenter.HasValue && !thumbsticksVals.isRightDeadZone && drawRightThumbstick)
            {
                spriteBatch.Draw(
                    thumbstick,
                    VirtualThumbsticks.RightThumbstickCenter.Value - new Vector2(thumbstick.Width / 2f, thumbstick.Height / 2f),
                    new Color(0, 50, 128, thumbstickAlpha));
            }
        }
    }
}
